//
//  ByViewDetailEvaluateHeader.m
//  oc_knowledge
//
//  Created by pro on 17/1/20.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByViewDetailEvaluateHeader.h"

@implementation ByViewDetailEvaluateHeader
{
    UILabel     *_titleLabel;   // 标题
    UIView      *_tagView;      // 标签
    UIButton    *_moreBtn;      // 更多按钮
    UIView      *_line;         // 线条

    
    BOOL        _needMore;    // 是否需要更多按钮
}
- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    // 标题
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.width - 10 * 2, 40)];
    _titleLabel.adjustsFontSizeToFitWidth = YES;
    _titleLabel.font = FONT(16);
    _titleLabel.text = @"商品评价";
    _titleLabel.textColor = [UIColor blackColor];
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    [self addSubview:_titleLabel];
    
    // 标签
    _tagView = [[UIView alloc] init];
    [self addSubview:_tagView];
    
    // 按钮
    _moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_moreBtn setImage:[UIImage imageNamed:@"product_detail_arrows_down"] forState:UIControlStateNormal];
    [_moreBtn setImage:[UIImage imageNamed:@"product_detail_arrows_up"] forState:UIControlStateSelected];
    [_moreBtn addTarget:self action:@selector(moreClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_moreBtn];

    // 线
    _line = [[UIView alloc] initWithFrame:CGRectMake(10, 0, self.width - 20, 0.5)];
    _line.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    [self addSubview:_line];
}

- (void)setTags:(NSArray *)tags {
    
    _tags = tags;
    
    if (_tags.count == 0 || !_tags) {
        return;
    }
    
    [self layoutCustomViews:NO];

}
- (void)layoutCustomViews:(BOOL)showMore {
    
    _tagView.frame = CGRectMake(0, _titleLabel.bottom, self.width, 25);
    
    _needMore = NO;
    
    [_tagView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    CGFloat x, y, width, height;
    CGFloat margin = 10;
    CGFloat offsetX = margin;
    CGFloat offsetY = 0;
    NSString *text;
    for (int i = 0; i < _tags.count; i ++) {
        text = _tags[i];
        width = [text getSizeOfString:14].width + 10;
        height = _tagView.height;
        x = offsetX;
        y = offsetY;
        if (offsetX + width >= _tagView.width - 10) {
            _needMore = YES;
            if (showMore) {
                offsetX = margin;
                offsetY += (height + margin);
                x = offsetX;
                y = offsetY;
            } else {
                break;
            }
        }
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
        label.backgroundColor = rgba(254, 210, 193, 1);
        label.font = FONT(12);
        label.text = text;
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.layer.cornerRadius = 5;
        label.layer.masksToBounds = YES;
        [_tagView addSubview:label];
        
        offsetX += (width + margin);
    }
    
    _tagView.height = _tagView.subviews.lastObject.bottom;
    
    
    if (_needMore) {
        _moreBtn.frame = CGRectMake(0, _tagView.bottom, self.width, 30);
        self.height = _moreBtn.bottom;
    } else {
        self.height = _tagView.bottom + 10;
    }
    
    _line.bottom = self.height;
}

- (void)moreClick:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    
    [self layoutCustomViews:sender.selected];
    
    if (self.moreClickBlock) {
        self.moreClickBlock();
    }
}

@end
